<?php

namespace System\Models;

class Users extends \System\Core\Model
{
     
    /**
     * Adds a user to the db.
     * @param type $user associative array with the user information, usually
     * $_POST.
     */
    public function addUser($user, $signUpPage = FALSE)
    {
        $salt = createPasswordSalt();
        $hash = createPasswordHash($salt, $user['password']);
        $ip = $this->input->ipLongAddr();

        // Check if the table is empty. If it is, auto confirm the user since
        // he will be the founder.
        $firstUser = ($this->userCount() == 0);
        if ($firstUser)
        {
            $founder = 1;
            $confirmed = 1;
        }
        else
        {
            $founder = 0;
            $confirmed = 0;
        }
        
        $passKey = $this->createPassKey();
        $data = array(
            'name'      =>  $user['userName'],
            'email'     =>  $user['email'],
            'salt'      =>  $salt,
            'password'  =>  $hash,
            'passkey'   =>  $passKey,
            'ip'        =>  $ip,
            'created'   =>  time(),
            'timezone'  =>  $user['timezone'],
            'founder'   =>  $founder,
            'confirmed' =>  $confirmed,
            'enabled'   =>  1
        );
        
        $this->db->insert('users', $data);
        
        $id = $this->db->insert_id();
        
        // Return info.
        return array('founder' => $firstUser, 'id' => $id, 'name' => $user['userName'], 'password' => $hash, 'salt' => $salt);
    }

    /**
     * Returns a tabel row containing the user that has the $id or FALSE if
     * the id isn't in the db.
     * @param type $id
     * @return mixed row as an object or FALSE if not found.
     */
    public function getUser($id)
    {
        $this->db->where('id', $id);
        $this->db->get('users');
        
        if ($this->db->num_rows() == 1)
        {
            return $this->db->row();
        }
        
        return FALSE;
    }
    
    /**
     * Returns the id that belongs to the user name.
     * @param string $name
     * @return mixed id of the user if available, else FALSE.
     */
    public function getIdForName($name)
    {
        if (empty($name))
        {
            return FALSE;
        }
        
        $this->db->where('name', $name);
        $this->db->get('users');
        
        if ($this->db->num_rows() == 1)
        {
            return $this->db->row()->id;
        }
        
        return FALSE;
    }
    
    /**
     * Returns the id that belongs to the email.
     * @param string $email
     * @return mixed id of the user if available, else FALSE.
     */
    public function getIdForEmail($email)
    {
        if (empty($email))
        {
            return FALSE;
        }
        
        $this->db->where('email', $email);
        $this->db->get('users');
        if ($this->db->num_rows() == 1)
        {
            return $this->db->row()->id;
        }
            
        return FALSE;
    }
        
    /**
     * Creates a passkey and makes sure that it's unique in the database.
     * @return string a 32 hex long passkey.
     */
    public function createPassKey()
    {
        do
        {
            $key = getPassKey();
            $this->db->where('passkey', $key);
            $this->db->get('users');
        } 
        while ($this->db->num_rows() > 0);
                
        return $key;
    }
    
    /**
     * Counts the total users in the database.
     * 
     * @param mixed $where Optional where parameter to limit the count.
     * @return integer total number of users. 
     */
    public function userCount($where = NULL)
    {
        if (!is_null($where))
        {
            $this->db->where($where);
        }
        $this->db->select('COUNT(*) AS total');
        $this->db->get('users');
        return $this->db->row()->total;        
    }
}

?>
